package com.wimcorp.magic.cost;

import javax.persistence.Entity;

import com.wimcorp.magic.abilities.Ability;
import com.wimcorp.magic.behaviour.Tapped;
import com.wimcorp.magic.cards.PlayingCard;
import com.wimcorp.magic.exception.CostException;
import com.wimcorp.magic.spell.Castable;

@Entity
public class TapCard extends Cost {

	public TapCard() {
	}

	public TapCard(TapCard cost) {
		super(cost);
	}

	public void payCost() throws CostException {
		Castable castable = getCastable();
		if (castable instanceof PlayingCard) {
			PlayingCard playingCard = (PlayingCard) castable;
			if(playingCard.hasBehaviour(Tapped.instance())){
				throw new CostException(String.format("%s was already tapped.",playingCard.getName()));
			}
			playingCard.addBehaviour(Tapped.instance());
		} else if (castable instanceof Ability) {
			PlayingCard playingCard = (PlayingCard) ((Ability) castable).getCard();
			if(playingCard.hasBehaviour(Tapped.instance())){
				throw new CostException(String.format("%s was already tapped.",playingCard.getName()));
			}
			((Ability) castable).getCard().addBehaviour(Tapped.instance());
		}
	}

	public String toString() {
		return "Tap card";

	}

	@Override
	public TapCard clone() {
		return new TapCard(this);
	}

}
